# ------------------------------------------------------------------------------
# Cargo Build Stage
# ------------------------------------------------------------------------------

FROM ekidd/rust-musl-builder:1.48.0 as cargo-build
USER root

ARG RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
ARG RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
RUN rustup default stable
RUN rustup target add x86_64-unknown-linux-musl

RUN mkdir -p /root/.cargo
COPY docker/.cargo/config /root/.cargo/
RUN export CARGO_HTTP_DEBUG=true
RUN export CARGO_LOG=cargo::ops::registry=debug
RUN mkdir -p /usr/src/rustcode
WORKDIR /usr/src/rustcode
COPY Cargo.toml Cargo.toml
RUN mkdir src/
RUN echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build -v --release --target=x86_64-unknown-linux-musl
RUN rm -f target/x86_64-unknown-linux-musl/release/deps/*
COPY src ./src
RUN RUSTFLAGS=-Clinker=musl-gcc cargo build -v --release --target=x86_64-unknown-linux-musl
# ------------------------------------------------------------------------------
# Final Stage
# ------------------------------------------------------------------------------

FROM alpine:latest

RUN addgroup -g 1000 myapp
RUN adduser -D -s /bin/sh -u 1000 -G myapp myapp
RUN mkdir -p /home/myapp/bin/
WORKDIR /home/myapp/bin/
COPY --from=cargo-build /usr/src/rustcode/target/x86_64-unknown-linux-musl/release/appservice-operator-demo .
RUN chown myapp:myapp appservice-operator-demo
USER myapp
EXPOSE 8080
CMD ["./appservice-operator-demo"]